package com.example.consumerone.controller;

import com.example.common.base.Result;
import com.example.consumerone.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "${com.smart.origins}")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/attentionUser")
    public Result<?> attentionUser(String userId, String beNoticedUserId){
        if (userService.attentionUser(userId, beNoticedUserId)){
            return Result.ok("关注成功");
        }
        return Result.error("关注失败");
    };

    @PostMapping("/reAttentionUser")
    public Boolean reAttentionUser(String userId, String beNoticedUserId){
        return userService.reAttentionUser(userId, beNoticedUserId);
    };

    @PostMapping("getCheckCode")
    public Result<?> getCheckCode(@RequestParam("userId") String userId){
        if (userService.getCheckCode(userId).equals("success")){
            return Result.ok("消息发送成功");
        }
        return Result.error("消息发送失败");
    }

    @PostMapping("/registered")
    public Result<?> registered(@RequestParam(name = "userId") String userId,
                                @RequestParam(name = "verificatioCode") String verificatioCode){
        Map<String, Object> registered = userService.registered(userId, verificatioCode);
        System.out.println(registered);
        if (registered.containsKey("error")){
            if (registered.get("error").equals("codeError")){
                return Result.error("codeError");
            }
            return Result.error("registError");
        }
        return Result.ok(registered);
    }

    @GetMapping("/test")
    public String test(){
        return "ok!";
    }

}
